<?php
namespace Portal\Controller;
use Common\Controller\HomebaseController;

class VideoController extends HomebaseController {
    //添加新卡种
	public function addvideo() {		
		$this->display(':addvideo');
	}
	public function upload(){
		for($i=1;$i<4;$i++){
			$file='file'.$i;
			if(file_exists($_FILES[$file]["tmp_name"])){
				if ((($_FILES[$file]["type"] == "image/gif") || ($_FILES[$file]["type"] == "image/jpeg") || ($_FILES[$file]["type"] == "image/pjpeg")|| ($_FILES[$file]["type"] == "image/png")|| ($_FILES[$file]["type"] == "image/bmp")) && ($_FILES[$file]["size"] < 1024000)) //定义文件类型，大小为1M以内
					{
					if ($_FILES[$file]["error"] > 0) 
					{
						echo "<script>alert('上传错误，请稍后再试！');history.go(-1);</script>";
						die();
					} else {
						$format='.'.substr($_FILES[$file]["type"],6);
						if(move_uploaded_file($_FILES[$file]["tmp_name"],VIDEO.'video'.date('U').$i.$format)){
							$pic[$i]=VIDEO.'video'.date('U').$i.$format;
							$picnum++;
						}else{
							echo "<script>alert('图片存储错误，请稍后再试！');history.go(-1);</script>";
							die();
						}
					}
				}else{
					echo "<script>alert('图片格式不正确（gif/jpeg/pjpeg/png）或超过限制（1M）！');history.go(-1);</script>";
					die();
				}
			}
		}
		$data['content']=$_POST['content'];
		$data['vpic']=json_encode($pic);
		$data['vtime']=date('U');
		//$picnum当前上传的图片张数
		$Video=M('video');
		$vid=$Video->add($data);
		echo "<script>alert('上传成功！');history.go(-1);</script>";
	}
	public function showvideo(){
		$Video=M('video');
		$video=$Video->order('vtime desc')->select();
		for($i=0;$i<count($video);$i++){
			$video[$i]['vpic']=json_decode($video[$i]['vpic'],true);
			$video[$i]['picnum']=count($video[$i]['vpic']);
			if($video[$i]['vpic'][1]){
				$video[$i]['vpic'][1]='/yujia'.substr($video[$i]['vpic'][1],1);
			}
			if($video[$i]['vpic'][2]){
				$video[$i]['vpic'][2]='/yujia'.substr($video[$i]['vpic'][2],1);
			}
			if($video[$i]['vpic'][3]){
				$video[$i]['vpic'][3]='/yujia'.substr($video[$i]['vpic'][3],1);
			}
			$video[$i]['vtime']=date('Y-m-d H:i',$video[$i]['vtime']);
		}
		$this->assign('video',$video);
		$this->display(':showvideo');
	}
}